mui.init({
    gestureConfig: {
        longtap: true, //默认为false
        release: true
    },
    pullRefresh : {
        container:"#refreshContainer",//下拉刷新容器标识，querySelector能定位的css选择器均可，比如：id、.class等
        down : {
            style:'circle',//必选，下拉刷新样式，目前支持原生5+ ‘circle’ 样式
            color:'#2BD009', //可选，默认“#2BD009” 下拉刷新控件颜色
            height:'50px',//可选,默认50px.下拉刷新控件的高度,
            range:'100px', //可选 默认100px,控件可下拉拖拽的范围
            offset:'0px', //可选 默认0px,下拉刷新控件的起始位置
            auto: false,//可选,默认false.首次加载自动上拉刷新一次
            callback :function () {
                newsDetail.getNewsDetail(newsDetail.uid);
                setTimeout(function () {
                    mui('#refreshContainer').pullRefresh().endPulldownToRefresh(false);
                },1000);
            }
        }
    }
});

var newsDetail;
var flag = false;
var flag2 = false;
mui.plusReady(function () {
	hideScrollBar();
	//返回全屏
	var old_back = mui.back;
	mui.back = function() {
	    plus.navigator.setFullscreen(false);
	   /* var A = plus.webview.getLaunchWebview();
		A.evalJS('window.location.reload()');*/
	    old_back();
	}
	//拦截侧滑返回事件
	var _self = plus.webview.currentWebview();
	_self.addEventListener("popGesture",function() {
	    plus.navigator.setFullscreen(false);
	});
	
	
	
    var newsId = plus.webview.currentWebview().newsid;
	var zhiwei = plus.webview.currentWebview().zhiwei;
    newsDetail = new Vue({
        el: '#newsDetailData',
        data: {
            questionBoxStatus:0,
            questionErrorBoxStatus:0,
            errorTipsContent:"数据加载失败，可能网络异常",
            uid:newsId,
            newsContent: [],
            vipSta:0,
            preandnext: [],
            collection_status: 'NO',
            related: [],
            loginSta:0,
            show: true
        },
        mounted() {
            this.getNewsDetail(newsId);
            this.updateStatus();
        },
        methods: {
            clickRefresh:function(){
                this.getNewsDetail(newsId);
                this.updateStatus();
            },
            updateStatus:function(){
                var api_host = plus.storage.getItem('HOST_URL');
                var self = this;
                mui.ajax({
                    url: api_host + '/api/linxuan/user/checkLoginStatus',
                    dataType: 'json',
                    headers:{'User-Token':plus.storage.getItem('UserToken')},
                    timeout:3000,
                    type: 'get',
                    success: function (msg) {
                        if (msg.code == 1) {
                            self.loginSta = 1;
                        } else {
                            self.loginSta = 0;
                            console.log(msg.msg);
                        }
                    },
                    error: function (xhr, type, errorThrown) {
                        console.log('登录状态请求失败请重试');
                    }
                });
            },
            getNewsDetail:function(newsId){
                showWatting();
                console.log('文章ID：' + newsId);
                var api_host = plus.storage.getItem('HOST_URL');
                var self = this;
                self.uid = newsId;
				if(zhiwei == 'zhiwei'){
					var url = api_host + '/api/linxuan/news/getposition';
				}else{
					var url = api_host + '/api/linxuan/news/getcontent';
				}
                mui.ajax({
                    url: url,
                    dataType: 'json',
                    data: {
                        'id': newsId,
                    },
					headers:{'User-Token':plus.storage.getItem('UserToken')},
                    type: 'get',
					timeout:8000,
                    success: function (msg) {
                        if (msg.code == 1) {
                            self.questionErrorBoxStatus = 0;
                            self.questionBoxStatus = 1;
							self.zhiwei = zhiwei;
                            self.newsContent = msg.data.content;
                            self.preandnext = msg.data.preandnext;
                            self.related = msg.data.related;
                            self.collection_status = msg.data.content.collection_status;
                            self.vipSta = msg.data.content.vipSta;

                            if (self.collection_status == 0) {
                                $('#collection_box_false').css('display', 'inline-block');
                                $('#collection_box_true').css('display', 'none');
                            } else if (self.collection_status == 1){
                                $('#collection_box_false').css('display', 'none');
                                $('#collection_box_true').css('display', 'inline-block');
                            }

                            self.$nextTick(function () {
                                $('body').animate({
                                    scrollTop: 0
                                }, 0);
                                if(plus.storage.getItem('newsFontSize') !== null){
                                    $(".content *").css("font-size", parseInt(plus.storage.getItem('newsFontSize')));
                                }else{
                                    $(".content *").css("font-size",14);
                                }

                                //长按保存图片
                                mui("#news_details_img").on('longtap', 'img', function () {
                                    var src = $(this).attr('src');
                                    downImg(src);
                                })
                                //点击预览
                                mui("#news_details_img").on('tap', 'img', function () {
                                    var src = $(this).attr('src');
                                    openBigPic([src]);
                                })
                                //点击链接使用浏览器打开
                                mui("#news_details_img").on('tap', 'a', function () {
                                    var href = $(this).attr('href');
                                    console.log('点击的链接为：'+href);
                                    var result = href.split('/');
                                    var hrefArr = [];
                                    var en;
                                    for(var i = 0; i < result.length; i++){
                                        hrefArr.push(result[i]);
                                    }
                                    if(hrefArr.indexOf('joinvip.html') > -1){
                                        mui.openWindow({
                                            url: '../member/memberVip.html',
                                            createNew: false,
                                            show: {
                                                autoShow: true,
                                                aniShow: 'pop-in',
                                                duration: 400
                                            },
                                            waiting: {
                                                autoShow: false,
                                            }
                                        });
                                    }else if(hrefArr.indexOf('kdjj') > -1 && hrefArr.indexOf('zwkd') > -1){
                                        en = hrefArr[hrefArr.length - 1].split('.html').join("");
                                        zhiwei = 'zhiwei';
                                        console.log('解析的ID为:'+en);
                                        self.getNewsDetail(parseInt(en));
                                    }else if(hrefArr.indexOf('kdjj') > -1 || hrefArr.indexOf('sszt') > -1 || hrefArr.indexOf('lxzt') > -1 || hrefArr.indexOf('lxzx') > -1){
                                        en = hrefArr[hrefArr.length - 1].split('.html').join("");
                                        zhiwei = '';
                                        console.log('解析的ID为:'+en);
                                        self.getNewsDetail(parseInt(en));
                                    }else{
                                        openDefaultUrl(href);
                                    }
                                })
                            })

                        } else {
                            self.questionErrorBoxStatus = 1;
                            self.questionBoxStatus = 0;
                            self.errorTipsContent = msg.msg;
                            mui.toast(msg.msg, {duration: '200',type: 'div'});
                        }
                    },
                    error: function (xhr, type, errorThrown) {
                        self.questionErrorBoxStatus = 1;
                        self.questionBoxStatus = 0;
                        mui.toast('加载失败!', {duration: '200',type: 'div'});
                    },
                    complete:function () {
                        plus.nativeUI.closeWaiting();
                    }
                });
            },
            addCollection:function(){
                var self = this;
                if(self.zhiwei == 'zhiwei'){
                    updateProductCollection('zhiwei', self.newsContent.id, self.newsContent.title, self.newsContent, self);
                }else{
                    updateProductCollection('article', self.newsContent.id, self.newsContent.title, self.newsContent, self);
                }
            },
            shareHref:function(){
                var self = this;
                globalShare(self.newsContent.title,self.newsContent.description,self.newsContent.url, plus.storage.getItem('HOST_URL')+'/upload/appimage/newsShare.png')
            },
            loginButton:function(){
                mui.openWindow({
                    url:'../../logReg/login.html',
                    id: 'loginHtml',
                    createNew:false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            },
            toTop: function() {
            	backTop();
            },
            up_vip: function () { //升级VIP
                mui.openWindow({
                    url: '../member/memberVip.html',
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            },
            moreFunc: function(){
            	if(!flag){
            		$('.popMask').show();
            		$('.popFunc').slideDown(100);
            	}else{
            		$('.popMask').hide();
            		$('.popFunc').slideUp(100);
            	}
            	flag = !flag;
            },
            closeFunc: function(){
            	if(flag){
            		$('.popMask').hide();
            		$('.popFunc').slideUp(100);
            	}else{
            		$('.popMask').show();
            		$('.popFunc').slideDown(100);
            	}
            	flag = !flag;
            },
            bigger: function(){
				var s_size = $(".content *").css("font-size"),s_number = parseFloat(s_size);
                if(s_number < 26){
                    var newSize = s_number +2;
                    $(".content *").css("font-size", newSize);
                    plus.storage.setItem('newsFontSize',newSize.toString());
                }else{
                    var newSize = 26;
                    $(".content *").css("font-size", newSize);
                    plus.storage.setItem('newsFontSize',newSize.toString());
                    mui.toast('字体已经不能再大咯！', {duration: '200',type: 'div'});
                }    
            },
            smaller: function(){
            	var s_size = $(".content *").css("font-size"),s_number = parseFloat(s_size);
                if(s_number > 14){
                    var newSize = s_number -2;
                    $(".content *").css("font-size", newSize);
                    plus.storage.setItem('newsFontSize',newSize.toString());
                }else{
                    var newSize = 14;
                    $(".content *").css("font-size", newSize);
                    plus.storage.setItem('newsFontSize',newSize.toString());
                    mui.toast('字体已经不能再小咯！', {duration: '200',type: 'div'});
                }  
            },
            white: function(){
            	new Broadcast().emit('say', {});
            	var obj = document.getElementById("white");
   				obj.setAttribute("href","../../../css/whiteSkin.css");
                plus.storage.setItem('newsTheme','white');
   				mui.toast('已切换到白天模式', {duration: '200',type: 'div'});
            },
            black: function(){
            	new Broadcast().emit('say', {});
            	var obj = document.getElementById("white");
   				obj.setAttribute("href","../../../css/blackSkin.css");
                plus.storage.setItem('newsTheme','black');
   				mui.toast('已切换到夜间模式', {duration: '200',type: 'div'});
            },
            fullScreen: function(){
            	this.show = !this.show;
            	var onOff = true;
                //控制全屏显示隐藏
                if(this.show == true){
                	plus.navigator.setFullscreen(false);
                }else{
                	plus.navigator.setFullscreen(true);
                }
            }
        }
    })
})

window.addEventListener('dataList',function(event){
    var embed = plus.webview.currentWebview();
    embed.reload(true);
});

//添加收藏
function addCollection() {
    var collection_info = newsDetail.newsContent;
	if(newsDetail.zhiwei == 'zhiwei'){
		updateProductCollection('zhiwei', newsDetail.newsContent.id, newsDetail.newsContent.title, collection_info, newsDetail);
	}else{
		updateProductCollection('article', newsDetail.newsContent.id, newsDetail.newsContent.title, collection_info, newsDetail);
	}
}

